/* NOTICE
 
    Jim - the Java Image Manager, copyright (c) 2005 - 2007 Grant Gardner 
 
    Jim is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Jim is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  
    In addition, as a special exception, you are permitted to link this code
    with the 3rd party libraries required to compile this file and to
    distribute a linked combination including them. You must obey the
    GNU General Public License in all respects for all of the code except for
    those 3rd party libraries that are licenced under terms incompatible with the
    GNU General Public License.  
 
 
 NOTICE */

/*
 * Created on 30/09/2004
 *
 */

package au.com.lastweekend.jim.io.metadata;

import java.util.Iterator;

import junitx.framework.ArrayAssert;

/**
 * @author grant@lastweekend.com.au
 * @version $Id: DirectoryAssert.java,v 1.3 2006/01/28 05:05:47 ggardner Exp $
 */
public final class DirectoryAssert {

    private DirectoryAssert() {

    };

    public static void assertDirectoriesEqual(Metadata expected, Metadata actual) {

        Iterator<Directory> expectedIter = expected.getDirectoryIterator();
        Iterator<Directory> actualIter = actual.getDirectoryIterator();

        while (expectedIter.hasNext()) {
            Directory expectedDir = expectedIter.next();
            if (!actualIter.hasNext()) {
                junit.framework.Assert.fail("Found " + expectedDir + " but not directory to match against");
            }
            Directory actualDir = actualIter.next();
            assertDirectoryEquals(expectedDir, actualDir);
        }
        if (actualIter.hasNext()) {
            Directory actualDir = actualIter.next();
            junit.framework.Assert.fail("Found extra directory " + actualDir);
        }
    }

    /**
     * @param expected
     * @param actual
     */
    public static void assertDirectoryEquals(Directory expected, Directory actual) {

        if (expected == null && actual == null) {
            return;
        }

        if (expected == null || actual == null) {
            junit.framework.Assert.fail("Expected: " + expected + " but found: " + actual);
        }
        junit.framework.Assert.assertEquals("Directory types", expected.getClass(), actual.getClass());

        // Assume that if the types are the same, then the names will be the
        // same
        String directoryName = actual.getName();
        junit.framework.Assert.assertEquals(directoryName + ": Tag count", expected.getTagCount(), actual.getTagCount());
        // Lets just assume that order is important, as we do try to preserve
        // it...
        Iterator<Tag> expectedIterator = expected.getTagIterator();
        Iterator<Tag> actualIterator = actual.getTagIterator();
        int tagNumber = 0;
        while (expectedIterator.hasNext() && actualIterator.hasNext()) {
            Tag expectedTag = expectedIterator.next();
            Tag actualTag = actualIterator.next();

            String tagDescription = directoryName + ": Tag number-" + tagNumber + " id-" + expectedTag.getTagTypeHex() + " ";
            junit.framework.Assert.assertEquals(tagDescription, expectedTag, actualTag);

            Object expectedValue = expected.getObject(expectedTag.getTagType());
            Object actualValue = actual.getObject(actualTag.getTagType());

            if (expectedValue instanceof Directory && actualValue instanceof Directory) {
                assertDirectoryEquals((Directory) expectedValue, (Directory) actualValue);
            } else if (expectedValue.getClass().isArray() && actualValue.getClass().isArray()) {
                if (expectedValue instanceof byte[] && actualValue instanceof byte[]) {
                    ArrayAssert.assertEquals(tagDescription + " byte[] values", (byte[]) expectedValue, (byte[]) actualValue);
                } else if (expectedValue instanceof int[] && actualValue instanceof int[]) {
                    ArrayAssert.assertEquals(tagDescription + " int[] values", (int[]) expectedValue, (int[]) actualValue);

                } else {
                    junit.framework.Assert.fail("Unable to compare " + expectedValue.getClass().getName() + " to "
                            + actualValue.getClass().getName());
                }
            } else {

                junit.framework.Assert.assertEquals(tagDescription + " value", expectedValue, actualValue);
            }

            tagNumber++;

        }

        Directory expectedLinked = expected.getLinkedDirectory();
        Directory actualLinked = expected.getLinkedDirectory();
        assertDirectoryEquals(expectedLinked, actualLinked);
    }
}